extends "res://BaseEnemy.gd"

# 森林蜘蛛特有属性
@export var web_cooldown = 4.0  # 蛛网冷却时间
@export var web_slow_factor = 0.4  # 蛛网减速效果
@export var web_slow_duration = 3.0  # 蛛网减速持续时间
@export var poison_damage = 2  # 毒素伤害
@export var poison_duration = 4.0  # 毒素持续时间
@export var poison_chance = 0.3  # 毒素几率

var web_timer = 0.0
var can_web = true

func _ready():
	# 调用父类的_ready方法
	super._ready()
	
	# 设置森林蜘蛛特有属性
	max_health = 30
	current_health = max_health
	movement_speed = 100.0
	damage = 6
	experience_value = 6
	
	# 更新生命值条
	if health_bar:
		health_bar.max_value = max_health
		health_bar.value = current_health
	
	# 设置外观
	if sprite:
		sprite.modulate = Color(0.6, 0.6, 0.6)  # 蜘蛛色调

func _physics_process(delta):
	# 调用父类的_physics_process方法
	super._physics_process(delta)
	
	# 处理蛛网冷却
	if not can_web:
		web_timer += delta
		if web_timer >= web_cooldown:
			can_web = true
			web_timer = 0.0
	
	# 如果可以使用蛛网且玩家存在，尝试使用蛛网
	if can_web and target_player and is_instance_valid(target_player):
		var distance = global_position.distance_to(target_player.global_position)
		if distance <= 120 and distance > 30:  # 在适当距离时使用蛛网
			shoot_web()

func shoot_web():
	if animation_player and animation_player.has_animation("web"):
		animation_player.play("web")
		
		# 创建蛛网投射物
		var web = preload("res://WebProjectile.tscn").instantiate()
		get_tree().get_root().add_child(web)
		web.global_position = global_position
		
		# 设置蛛网属性
		var direction = (target_player.global_position - global_position).normalized()
		web.direction = direction
		web.speed = 150.0
		web.slow_factor = web_slow_factor
		web.slow_duration = web_slow_duration
		web.source = self
		
		# 重置蛛网冷却
		can_web = false
		web_timer = 0.0
		
		print("蜘蛛发射蛛网!")

# 重写攻击方法，添加毒素效果
func attack_player():
	# 调用父类的攻击方法
	super.attack_player()
	
	# 尝试造成毒素效果
	if target_player and is_instance_valid(target_player) and randf() < poison_chance:
		if target_player.has_method("apply_poison_effect"):
			target_player.apply_poison_effect(poison_damage, poison_duration)
		else:
			# 如果玩家没有毒素方法，创建一个简单的毒素效果
			var poison_effect = preload("res://PoisonEffect.tscn").instantiate()
			get_tree().get_root().add_child(poison_effect)
			poison_effect.target = target_player
			poison_effect.damage_per_tick = poison_damage
			poison_effect.duration = poison_duration
		
		print("蜘蛛的攻击造成了毒素效果!")
